﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using Telerik.Web.UI;


namespace Sistema_de_Taller.SDT_UI.Client
{
    public partial class client_default : System.Web.UI.UserControl
    {
        public static DataTable VehiclesTMP;

        private void Page_Init(object sender, System.EventArgs e)
        {


            if (!IsPostBack)
            {
                VehiclesTMP = CreateDataTable();
                Session["myDatatable"] = VehiclesTMP;
                this.RadGrid1.DataSource = ((DataTable)Session["myDatatable"]).DefaultView;
                //this.RadGrid1.DataSource = VehiclesTMP;
                //this.RadGrid1.DataBind();
                //this.RadGrid1.Rebind(); 
            }
            
            this.RadGrid1.Rebind(); 

        }

        protected void Page_Load(object sender, EventArgs e)
        {
            this.RadGrid1.Rebind();

        }

        protected void btnCreateClient_Click(object sender, EventArgs e)
        {

            MessageOk.Visible = false;
            Common_Files.Definitions.Client Client = new Common_Files.Definitions.Client();

            Client.Client_Name = txtName.Text;
            Client.Client_Social = txtSocial.Text;
            Client.Client_State = txtState.Text;
            Client.Client_Residence = txtResidence.Text;
            Client.Client_Email = txtMail.Text;
            Client.Client_DateUp = dpDateUp.SelectedDate.Value;
            Client.Client_Country = txtCountry.Text;
            Client.Client_Province = txtProvince.Text;
            Client.Client_City = txtCity.Text;
            Client.Client_PostCode = txtPostCode.Text;
            Client.Client_Phone = txtPhone.Text;

            SDT_DAL.Utilities.Client_DAL inserto = new SDT_DAL.Utilities.Client_DAL();

            Client.Client_ID = inserto.ClientInsert(Client);

            insertVehicle(Client.Client_ID);

            lblMessages.Text = "Cliente cargado satisfactoriamente";
            MessageOk.Visible = true;

        }

        public void insertVehicle(UInt32 Client_ID)
        {
            foreach (DataRow Fila in VehiclesTMP.Rows)
            {
                Common_Files.Definitions.Vehicle Vehicle = new Common_Files.Definitions.Vehicle();
                Vehicle.Client_ID = Client_ID;
                Vehicle.Vehicle_Brand = Fila["Marca"].ToString();
                Vehicle.Vehicle_Model = Fila["Modelo"].ToString();
                Vehicle.Vehicle_Patent = Fila["Patente"].ToString();
                Vehicle.Vehicle_EngineNumber = Fila["Número de Motor"].ToString();
                Vehicle.Vehicle_SerialNumber = Convert.ToUInt32(Fila["Número de Serie"]);
                Vehicle.Vehicle_SellDate = Convert.ToDateTime(Fila["Fecha de Venta"]);
                SDT_DAL.Utilities.Vehicle_DAL inserto = new SDT_DAL.Utilities.Vehicle_DAL();
                inserto.VehicleInsert(Vehicle);
            }
        }

        protected void btnAddVehicle_Click(object sender, EventArgs e)
        {
            this.RadGrid1.DataSource = VehiclesTMP;
            this.RadGrid1.Rebind();
            btnCreateVehicle.Visible = false;
            pnlAddVehicle.Visible = true;
        }

        protected void dpSellDate_Load(object sender, EventArgs e)
        {
            dpSellDate.SelectedDate = DateTime.Today;
        }

        protected void dpDateUp_Load(object sender, EventArgs e)
        {
            dpDateUp.SelectedDate = DateTime.Today;
        }

        protected void dgvVehicles_Load(object sender, EventArgs e)
        {

        }

        private DataTable AddVehicle()
        {
            //DataTable VehiclesTMP = new DataTable();
            // VehiclesTMP = CreateDataTable();
            DataRow Fila = VehiclesTMP.NewRow();
            Fila["Marca"] = cmbBrand.SelectedValue.ToString();
            Fila["Modelo"] = txtModel.Text;
            Fila["Patente"] = txtPatent.Text;
            Fila["Número de Serie"] = Convert.ToUInt32(txtSerialNumber.Text);
            Fila["Número de Motor"] = txtEngineNumber.Text;
            Fila["Fecha de Venta"] = dpSellDate.SelectedDate;
            VehiclesTMP.Rows.Add(Fila);
            return (VehiclesTMP);
        }

        protected void btnNewVehicle_Click(object sender, EventArgs e)
        {
            DataTable VehiclesTMP = AddVehicle();
            this.RadGrid1.DataSource = VehiclesTMP;
            this.RadGrid1.Rebind();
            btnCreateVehicle.Visible = true;
        }

        private DataTable CreateDataTable()
        {
            DataTable VehiclesTMP = new DataTable();

            DataColumn Vehicle_Brand;
            DataColumn Vehicle_Patent;
            DataColumn Vehicle_EngineNumber;
            DataColumn Vehicle_Model;
            DataColumn Vehicle_SerialNumber;
            DataColumn Vehicle_SellDate;


            Vehicle_Brand = new DataColumn();
            Vehicle_Brand.DataType = Type.GetType("System.String");
            Vehicle_Brand.ColumnName = "Marca";
            VehiclesTMP.Columns.Add(Vehicle_Brand);

            Vehicle_Patent = new DataColumn();
            Vehicle_Patent.DataType = Type.GetType("System.String");
            Vehicle_Patent.ColumnName = "Patente";
            VehiclesTMP.Columns.Add(Vehicle_Patent);

            Vehicle_EngineNumber = new DataColumn();
            Vehicle_EngineNumber.DataType = Type.GetType("System.String");
            Vehicle_EngineNumber.ColumnName = "Número de Motor";
            VehiclesTMP.Columns.Add(Vehicle_EngineNumber);

            Vehicle_Model = new DataColumn();
            Vehicle_Model.DataType = Type.GetType("System.String");
            Vehicle_Model.ColumnName = "Modelo";
            VehiclesTMP.Columns.Add(Vehicle_Model);

            Vehicle_SerialNumber = new DataColumn();
            Vehicle_SerialNumber.DataType = Type.GetType("System.UInt32");
            Vehicle_SerialNumber.ColumnName = "Número de Serie";
            VehiclesTMP.Columns.Add(Vehicle_SerialNumber);

            Vehicle_SellDate = new DataColumn();
            Vehicle_SellDate.DataType = Type.GetType("System.DateTime");
            Vehicle_SellDate.ColumnName = "Fecha de Venta";
            VehiclesTMP.Columns.Add(Vehicle_SellDate);

            return VehiclesTMP;
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            pnlAddVehicle.Visible = false;
            this.RadGrid1.Rebind();
        }

 

    }
}